package com.evan.review.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;

import com.evan.review.R;

/**
 * Fragment通信方式演示
 * 展示Fragment之间以及Fragment与Activity之间的通信方式
 */
public class CommunicationActivity extends AppCompatActivity implements SenderFragment.MessageSendListener {

    private TextView tvActivityLog;
    private SenderFragment senderFragment;
    private ReceiverFragment receiverFragment;
    private Button btnInterface;
    private Button btnViewModel;
    private Button btnBundle;
    private Button btnBroadcast;
    private SharedViewModel viewModel;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_communication);
        
        // 初始化ViewModel
        viewModel = new ViewModelProvider(this).get(SharedViewModel.class);
        
        initViews();
        setupObservers();
        
        if (savedInstanceState == null) {
            addInitialFragments();
        }
    }
    
    private void initViews() {
        tvActivityLog = findViewById(R.id.tv_activity_log);
        btnInterface = findViewById(R.id.btn_interface);
        btnViewModel = findViewById(R.id.btn_viewmodel);
        btnBundle = findViewById(R.id.btn_bundle);
        btnBroadcast = findViewById(R.id.btn_broadcast);
        
        btnInterface.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logMessage("接口通信演示: SenderFragment将通过接口回调发送消息");
                senderFragment.sendMessageViaInterface("通过接口发送的消息");
            }
        });
        
        btnViewModel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logMessage("ViewModel通信演示: 通过共享ViewModel发送消息");
                viewModel.setMessage("通过ViewModel发送的消息");
            }
        });
        
        btnBundle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logMessage("Bundle通信演示: 通过Fragment Arguments传递数据");
                replaceReceiverWithBundle("通过Bundle传递的消息");
            }
        });
        
        btnBroadcast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logMessage("广播通信演示: 通过本地广播发送消息");
                senderFragment.sendMessageViaBroadcast("通过广播发送的消息");
            }
        });
    }
    
    private void setupObservers() {
        // 观察ViewModel中的数据变化
        viewModel.getMessage().observe(this, message -> {
            logMessage("Activity接收到ViewModel消息: " + message);
        });
    }
    
    private void addInitialFragments() {
        // 添加发送方Fragment
        senderFragment = new SenderFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.sender_container, senderFragment, "sender_fragment");
        
        // 添加接收方Fragment
        receiverFragment = new ReceiverFragment();
        transaction.add(R.id.receiver_container, receiverFragment, "receiver_fragment");
        
        transaction.commit();
    }
    
    private void replaceReceiverWithBundle(String message) {
        // 创建带有Bundle参数的新Fragment
        ReceiverFragment newFragment = ReceiverFragment.newInstance(message);
        
        // 替换接收方Fragment
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.receiver_container, newFragment);
        transaction.commit();
        
        // 更新引用
        receiverFragment = newFragment;
    }
    
    @Override
    public void onMessageSent(String message) {
        // 通过接口接收来自Fragment的消息
        logMessage("Activity通过接口接收到消息: " + message);
        
        // 将消息转发给接收方Fragment
        if (receiverFragment != null && receiverFragment.isAdded()) {
            receiverFragment.receiveMessage("Activity转发: " + message);
        }
    }
    
    // 记录消息到界面
    public void logMessage(String message) {
        String currentLog = tvActivityLog.getText().toString();
        tvActivityLog.setText((currentLog.isEmpty() ? "" : currentLog + "\n") + message);
    }
} 